package cn.ms.doctor.Controller;

import cn.ms.doctor.Entity.Patient;
import cn.ms.doctor.Service.PatientService;
import cn.ms.doctor.Utils.DataStandardUtils;
import cn.ms.doctor.Utils.GlobalMessage;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/Patient")
public class PatientController {

    @Resource
    PatientService patientService;

    @RequestMapping("/isLogin/isSystemAndDoctor/getPatientBySearch")
    public Object getPatientBySearch(@RequestBody Map<String,Object> params){
        if (DataStandardUtils.standardKeyIsEmpty(params,"pageIndex","pageSize")) {
            return new GlobalMessage(200,1,"非法请求...",null);
        }
        Map<String,Object> data=patientService.getPatientBySearch(params);
        return new GlobalMessage(200,0,null,data);
    }

    @RequestMapping("/isLogin/isSystemAdmin/updatePatientNameOrIdnumOrSex")
    public Object updatePatientNameOrIdnum(@RequestBody Patient patient){
        if (StringUtils.isEmpty(patient.getPatientId())&&StringUtils.isEmpty(patient.getPatientName())
            ||StringUtils.isEmpty(patient.getPatientId())&&StringUtils.isEmpty(patient.getPatientIdnum())
            ||StringUtils.isEmpty(patient.getPatientId())&&StringUtils.isEmpty(patient.getPatientSex())) {
            return new GlobalMessage(200,1,"非法请求...",null);
        }
        if (!patientService.updatePatientNameOrIdnumOrSex(patient)) {
            return new GlobalMessage(200,0,"出现异常,请联系管理员...",null);
        }
        return new GlobalMessage(200,0,null,"修改成功...");
    }
    @RequestMapping("/isLogin/isSystemAndDoctor/addPatient")
    public Object addPatient(@RequestBody Patient patient){
        if (StringUtils.isEmpty(patient.getPatientName())||StringUtils.isEmpty(patient.getPatientSex())||StringUtils.isEmpty(patient.getPatientIdnum())) {
            return new GlobalMessage(200,2,"非法请求...",null);
        }
        if (patientService.getPatientCountByIdnum(patient.getPatientIdnum())!=0){
            return new GlobalMessage(200,1,"您输入的身份证已经有患者了哦...",null);
        }
        if (!patientService.addPatient(patient)){
            return new GlobalMessage(200,0,"出现异常,请联系管理员...",null);
        }
        return new GlobalMessage(200,0,null,"添加成功...");
    }

    @RequestMapping("/isLogin/isSystemAdmin/deletePatientByIdAndIdnum")
    public Object deletePatientByIdAndIdnum(@RequestBody Patient patient){
        if (StringUtils.isEmpty(patient.getPatientId())||StringUtils.isEmpty(patient.getPatientIdnum())) {
            return new GlobalMessage(200,2,"非法请求...",null);
        }
        if (!patientService.deletePatientByIdAndIdnum(patient)){
            return new GlobalMessage(200,0,"出现异常或者参数不正确,请联系管理员...",null);
        }
        return new GlobalMessage(200,0,null,"删除成功...");
    }
}
